package com.gp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gp.mapper.BaseUserrelationMapper;
import com.gp.model.dto.DistributorInfo;
import com.gp.model.entity.BaseUserrelation;
import com.gp.service.IBaseUserrelationService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 弓成龙
 * @since 2017-11-26
 */
@Service
public class BaseUserrelationServiceImpl extends ServiceImpl<BaseUserrelationMapper, BaseUserrelation>
		implements IBaseUserrelationService {

	@Autowired
	private BaseUserrelationMapper relationMapper;

	@Override
	public boolean insertRelation(Long uid, String parentId) {
		BaseUserrelation relation = new BaseUserrelation(uid, parentId);
		Map<String, Object> map = new HashMap();
		map.put("usercode", parentId);
		List<BaseUserrelation> userrelation = relationMapper.selectByMap(map);
		if (userrelation.isEmpty()) {
			return false;
		}
		BaseUserrelation re = userrelation.get(0);
		Integer ulevel = Integer.valueOf(re.getLevel()) - 1;
		relation.setLevel(ulevel.toString());
		return relationMapper.insertRelation(relation) == 1;
	}

  @Override
  public List<DistributorInfo> listLowers(Long userId) {
    return relationMapper.getlowers(userId);
  }

}
